package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.WikiClassActivity;
import com.ihighest.app.infox.activity.WikiContentListActivity;

/**
 * 种植百科右侧数据适配器
 * 
 * @author yyh Created at 2015-10-13 下午5:52:09
 * @version 1.0
 */
public class PlantRightListViewAdapter extends BaseAdapter {
	LayoutInflater inflater;
	Context context;
	JSONArray array;
	String TAG = "PlantRightListViewAdapter";

	// ImageLoader imageLoader = ImageLoader.getInstance();

	public PlantRightListViewAdapter(Context context, JSONArray array) {
		super();
		this.context = context;
		this.array = array;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return array != null ? array.length() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_fragment_plantlist_itemlayout, null);
			holder.plant_item_icon = (ImageView) convertView.findViewById(R.id.iv_plant_icon);
			holder.plant_item_name = (TextView) convertView.findViewById(R.id.tv_plant_name);
			holder.iv_btn_next_goto = (ImageView) convertView.findViewById(R.id.iv_btn_next_goto);
			holder.ll_btn_goto = (LinearLayout) convertView.findViewById(R.id.ll_btn_goto);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			JSONObject json = array.getJSONObject(position);
			if (!TextUtils.isEmpty(json.getString("image"))) {

				// DisplayImageOptions defaultOptions = new
				// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
				// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong)
				// .cacheInMemory(true).cacheOnDisc(true).build();
				// imageLoader.displayImage(json.getString("image"),
				// holder.plant_item_icon, defaultOptions);
				AppKit.SetNormalImg(json.getString("image"), holder.plant_item_icon);
			} else {
				holder.plant_item_icon.setVisibility(View.GONE);
			}
			holder.plant_item_name.setText(json.getString("title"));
		} catch (Exception ex) {
			Log.e(TAG, ex.getMessage(), ex);
		}
		// 点击事件
		holder.ll_btn_goto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				try {
					if (array.getJSONObject(position).getBoolean("leaf")) {
						Intent intent = new Intent(context, WikiContentListActivity.class);
						intent.putExtra("id", array.getJSONObject(position).getString("id"));
						context.startActivity(intent);
					} else {
						Intent intent = new Intent(context, WikiClassActivity.class);
						intent.putExtra("id", array.getJSONObject(position).getString("id"));
						context.startActivity(intent);
					}
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);

				}
			}
		});
		return convertView;
	}

	class ViewHolder {

		ImageView plant_item_icon, iv_btn_next_goto;
		TextView plant_item_name;
		LinearLayout ll_btn_goto;
	}
}
